<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class banner extends CI_Controller
{
	protected $_templates;
	public function __construct()
	{
        parent::__construct();
		$this->load->model('banner_model','banner');
		//$this->load->config('config_probanner');
		$this->load->library('Image_lib');
		$this->load->helper('icon');
		$this->load->model('bannerMainCat_model','bannercat');
		$this->pre_message = "";
		$this->permit_library->check_permit();
		$this->session->set_userdata(array('Url'=>uri_string()));           
	}
	
	function index(){}
	function write_probanner()
	{
		$str = "<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n/**\n* Cache file for config_probanner cafenika.com.\n* Date: ".date('d/m/y H:i:s').".\n**/";
		$str .= "\n";
		$list = $this->probanner->get_write_banner();
		$total = count($list);
		$str .= "\n\$config['total_probanner'] = '$total';";
		$k=1;
		for($i=0;$i < count($list);$i++)
		{
			$rs = $list[$i];
			$title = str_replace("'","\'",$rs->BannerTitle);
			$img = str_replace("'","\'",$rs->BannerImg);
			$link = str_replace("'","\'",$rs->BannerLink);
			$str .= "\n\$config['probanner_title_$k'] = '$title';";
			$str .= "\n\$config['probanner_img_$k'] = '$img';";
			$str .= "\n\$config['probanner_link_$k'] = '$link';";          
			$k++;
		}
		$str .= "\n\n/* End of file support cafenika.com*/\n/* Location: ./cf/config_probanner.php */";
		write_file('../cf/config_probanner.php', $str);        
	}
	/*==================bannermain====================*/
	function bannermain()
	{
		//$this->write_probanner();
		$data['title'] = 'Quản lý banner sản phẩm';
		$data['add'] = 'banner/add_bannermain';
		$config['base_url'] = base_url().'banner/bannermain/';  
		$config['total_rows']   =  $this->banner->getNumBanner();
		$data['num'] = $config['total_rows'];
		$config['per_page']  =   20;
		$config['uri_segment'] = 3; 
		$this->pagination->initialize($config);   
		$data['list'] =   $this->banner->getAllBanner($config['per_page'],$this->uri->segment('3'));
		$data['pagination']    = $this->pagination->create_links();           
		$this->_templates['page'] = 'banner/bannermain/list';
		$this->site->load($this->_templates['page'],$data);          
	}
	function add_bannermain()
	{
		$data['title'] = 'Thêm mới Banner sản phẩm';
		$data['listbannercat'] = $this->bannercat->getListCat();
		if(!empty($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('BannerTitle','Tiêu đề','trim|required');  
			$this->form_validation->set_rules('BannerImg','Hình ảnh','trim|required');  
			$this->form_validation->set_rules('BannerLink','Link','trim|required'); 
			//$this->form_validation->set_rules('TownID','Tỉnh thành','trim|required');
			
			if($this->form_validation->run() == FALSE)
			{
				$this->pre_message = validation_errors();
			}
			else
			{
				$BannerImg = $this->input->post('BannerImg');
				
				if($this->banner->saveBanner($BannerImg)){
					$this->session->set_flashdata('message','Lưu thành công');
					redirect('banner/bannermain');
				}else{
					$this->pre_message = 'Lưu không thành công';
				}
			} 
		}
		
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'banner/bannermain/add';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function edit_bannermain()
	{
		$data['id'] = $this->uri->segment(3);
		$data['listbannercat'] = $this->bannercat->getListCat();
		$data['rs'] = $this->banner->get_banner_by_id($data['id']);
		$data['bannertitle'] = ($this->input->post('BannerTitle')!='')?$this->input->post('BannerTitle'): $data['rs']->BannerTitle;
		$data['idBannerCat'] = ($this->input->post('ParentID')!='')?$this->input->post('ParentID'): $data['rs']->idBannerCat;
		$data['bannerimg'] = ($this->input->post('BannerImg')!='')?$this->input->post('BannerImg'): $data['rs']->BannerImg;
		$data['bannerlink'] = ($this->input->post('BannerLink')!='')?$this->input->post('BannerLink'): $data['rs']->BannerLink;
		//$data['townid'] = ($this->input->post('TownID')!='')?$this->input->post('TownID'): $data['rs']->TownID;
		$data['bl_active'] = ($this->input->post('bl_active')!='')?$this->input->post('bl_active'):$data['rs']->bl_active;
		$data['title'] = 'Thêm mới Banner sản phẩm';
		
		if(!empty($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('BannerTitle','Tiêu đề','trim|required');  
			$this->form_validation->set_rules('BannerImg','Hình ảnh','trim|required');  
			$this->form_validation->set_rules('BannerLink','Link','trim|required'); 
			
			if($this->form_validation->run() == FALSE)
			{
				$this->pre_message = validation_errors();
			}
			else
			{
				$BannerImg = $this->input->post('BannerImg');
				if($this->banner->saveBanner($BannerImg))
				{
					 $this->session->set_flashdata('message','Lưu thành công');
					 redirect('banner/bannermain');
				}
				else
				{
					 $this->pre_message = 'Lưu không thành công';
				}
			} 
		}
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'banner/bannermain/edit';            
		$this->site->load($this->_templates['page'],$data);
	} 

	function del_bannermain()
	{
		$id = $this->uri->segment(3);
		$page = $this->uri->segment(4);
		if($this->banner->delete($id))
			$this->session->set_flashdata('message','Đã xóa thành công');
		else $this->session->set_flashdata('message','Xóa không thành công');
		redirect('banner/bannermain/'.$page);
	}

	function dels_bannermain()
	{
		if(!empty($_POST['ar_id']))
		{
			$page = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
			
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->banner->delete($ar_id[$i]))
							$this->session->set_flashdata('message','Đã xóa thành công');
						else $this->session->set_flashdata('message','Xóa không thành công');
					}
				}
			}
		}
		redirect('banner/bannermain/'.$page);
	} 
	
	/*==================bannermain====================*/
	function bannerright()
	{
		//$this->write_probanner();
		$data['title'] = 'Quản lý banner sản phẩm';
		$data['add'] = 'banner/add_banner_right';
		$config['base_url'] = base_url().'banner/bannerright/';  
		$config['total_rows']   =  $this->banner->getNumBanner_right();
		$data['num'] = $config['total_rows'];
		$config['per_page']  =   20;
		$config['uri_segment'] = 3; 
		$this->pagination->initialize($config);   
		$data['list'] =   $this->banner->getAllBanner_right($config['per_page'],$this->uri->segment('3'));
		$data['pagination']    = $this->pagination->create_links();           
		$this->_templates['page'] = 'banner/banner_right/list';
		$this->site->load($this->_templates['page'],$data);          
	}
	function add_banner_right()
	{
		$data['title'] = 'Thêm mới Banner sản phẩm';
		if(!empty($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('BannerTitle','Tiêu đề','trim|required');  
			$this->form_validation->set_rules('BannerImg','Hình ảnh','trim|required');  
			$this->form_validation->set_rules('BannerLink','Link','trim|required'); 
			//$this->form_validation->set_rules('TownID','Tỉnh thành','trim|required');
			
			if($this->form_validation->run() == FALSE)
			{
				$this->pre_message = validation_errors();
			}
			else
			{
				$BannerImg = $this->input->post('BannerImg');
				
				if($this->banner->saveBanner_right($BannerImg)){
					$this->session->set_flashdata('message','Lưu thành công');
					redirect('banner/bannerright');
				}else{
					$this->pre_message = 'Lưu không thành công';
				}
			} 
		}
		
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'banner/banner_right/add';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function edit_banner_right()
	{
		$data['id'] = $this->uri->segment(3);
		$data['rs'] = $this->banner->get_banner_right_by_id($data['id']);
		$data['bannertitle'] = ($this->input->post('BannerTitle')!='')?$this->input->post('BannerTitle'): $data['rs']->BannerTitle;
		$data['bannerimg'] = ($this->input->post('BannerImg')!='')?$this->input->post('BannerImg'): $data['rs']->BannerImg;
		$data['bannerlink'] = ($this->input->post('BannerLink')!='')?$this->input->post('BannerLink'): $data['rs']->BannerLink;
		//$data['townid'] = ($this->input->post('TownID')!='')?$this->input->post('TownID'): $data['rs']->TownID;
		$data['bl_active'] = ($this->input->post('bl_active')!='')?$this->input->post('bl_active'):$data['rs']->bl_active;
		$data['title'] = 'Thêm mới Banner sản phẩm';
		
		if(!empty($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('BannerTitle','Tiêu đề','trim|required');  
			$this->form_validation->set_rules('BannerImg','Hình ảnh','trim|required');  
			$this->form_validation->set_rules('BannerLink','Link','trim|required'); 
			
			if($this->form_validation->run() == FALSE)
			{
				$this->pre_message = validation_errors();
			}
			else
			{
				$BannerImg = $this->input->post('BannerImg');
				if($this->banner->saveBanner_right($BannerImg))
				{
					 $this->session->set_flashdata('message','Lưu thành công');
					 redirect('banner/bannerright');
				}
				else
				{
					 $this->pre_message = 'Lưu không thành công';
				}
			} 
		}
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'banner/banner_right/edit';            
		$this->site->load($this->_templates['page'],$data);
	} 

	function del_banner_right()
	{
		$id = $this->uri->segment(3);
		$page = $this->uri->segment(4);
		if($this->banner->delete_banner_right($id))
			$this->session->set_flashdata('message','Đã xóa thành công');
		else $this->session->set_flashdata('message','Xóa không thành công');
		redirect('banner/bannerright/'.$page);
	}

	function dels_banner_right()
	{
		if(!empty($_POST['ar_id']))
		{
			$page = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
			
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->banner->delete_banner_right($ar_id[$i]))
							$this->session->set_flashdata('message','Đã xóa thành công');
						else $this->session->set_flashdata('message','Xóa không thành công');
					}
				}
			}
		}
		redirect('banner/bannerright/'.$page);
	}    
	
	/*==================banner bottom ====================*/
	function bannerbottom()
	{
		//$this->write_probanner();
		$data['title'] = 'Quản lý banner sản phẩm';
		$data['add'] = 'banner/add_banner_bottom';
		$config['base_url'] = base_url().'banner/bannerbottom/';  
		$config['total_rows']   =  $this->banner->getNumBanner_bottom();
		$data['num'] = $config['total_rows'];
		$config['per_page']  =   20;
		$config['uri_segment'] = 3; 
		$this->pagination->initialize($config);   
		$data['list'] =   $this->banner->getAllBanner_bottom($config['per_page'],$this->uri->segment('3'));
		$data['pagination']    = $this->pagination->create_links();           
		$this->_templates['page'] = 'banner/banner_bottom/list';
		$this->site->load($this->_templates['page'],$data);          
	}
	function add_banner_bottom()
	{
		$data['title'] = 'Thêm mới Banner sản phẩm';
		if(!empty($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('BannerTitle','Tiêu đề','trim|required');  
			$this->form_validation->set_rules('BannerImg','Hình ảnh','trim|required');  
			$this->form_validation->set_rules('BannerLink','Link','trim|required'); 
			//$this->form_validation->set_rules('TownID','Tỉnh thành','trim|required');
			
			if($this->form_validation->run() == FALSE)
			{
				$this->pre_message = validation_errors();
			}
			else
			{
				$BannerImg = $this->input->post('BannerImg');
				
				if($this->banner->saveBanner_bottom($BannerImg)){
					$this->session->set_flashdata('message','Lưu thành công');
					redirect('banner/bannerbottom');
				}else{
					$this->pre_message = 'Lưu không thành công';
				}
			} 
		}
		
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'banner/banner_bottom/add';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function edit_banner_bottom()
	{
		$data['id'] = $this->uri->segment(3);
		$data['rs'] = $this->banner->get_banner_bottom_by_id($data['id']);
		$data['bannertitle'] = ($this->input->post('BannerTitle')!='')?$this->input->post('BannerTitle'): $data['rs']->BannerTitle;
		$data['bannerimg'] = ($this->input->post('BannerImg')!='')?$this->input->post('BannerImg'): $data['rs']->BannerImg;
		$data['bannerlink'] = ($this->input->post('BannerLink')!='')?$this->input->post('BannerLink'): $data['rs']->BannerLink;
		//$data['townid'] = ($this->input->post('TownID')!='')?$this->input->post('TownID'): $data['rs']->TownID;
		$data['bl_active'] = ($this->input->post('bl_active')!='')?$this->input->post('bl_active'):$data['rs']->bl_active;
		$data['title'] = 'Thêm mới Banner sản phẩm';
		
		if(!empty($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('BannerTitle','Tiêu đề','trim|required');  
			$this->form_validation->set_rules('BannerImg','Hình ảnh','trim|required');  
			$this->form_validation->set_rules('BannerLink','Link','trim|required'); 
			
			if($this->form_validation->run() == FALSE)
			{
				$this->pre_message = validation_errors();
			}
			else
			{
				$BannerImg = $this->input->post('BannerImg');
				if($this->banner->saveBanner_bottom($BannerImg))
				{
					 $this->session->set_flashdata('message','Lưu thành công');
					 redirect('banner/bannerbottom');
				}
				else
				{
					 $this->pre_message = 'Lưu không thành công';
				}
			} 
		}
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'banner/banner_bottom/edit';            
		$this->site->load($this->_templates['page'],$data);
	} 

	function del_banner_bottom()
	{
		$id = $this->uri->segment(3);
		$page = $this->uri->segment(4);
		if($this->banner->delete_banner_bottom($id))
			$this->session->set_flashdata('message','Đã xóa thành công');
		else $this->session->set_flashdata('message','Xóa không thành công');
		redirect('banner/bannerbottom/'.$page);
	}

	function dels_banner_bottom()
	{
		if(!empty($_POST['ar_id']))
		{
			$page = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
			
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->banner->delete_banner_bottom($ar_id[$i]))
							$this->session->set_flashdata('message','Đã xóa thành công');
						else $this->session->set_flashdata('message','Xóa không thành công');
					}
				}
			}
		}
		redirect('banner/bannerbottom/'.$page);
	}           
}
?>
